package com.boot.controller;

import com.boot.CheckPermission;
import com.boot.entity.Notify;
import com.boot.entity.User;
import com.boot.service.impl.INotiryManagementServiceImpl;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * 公告管理Controller
 */
@Controller
@RequestMapping("/notify")
public class NotifyManagementController {

    @Autowired
    INotiryManagementServiceImpl service;

    @RequestMapping("/query")
    public String queryNotify(Integer pageIndex, Integer pageSize, Model model) {
        if (pageIndex == null) {
            pageIndex = 1;
        }
        if (pageSize == null) {
            pageSize = 6;
        }
        PageHelper.startPage(pageIndex, pageSize);
        List<Notify> notifies = service.queryAllNotify(1);
        model.addAttribute("notifies", notifies);
        int count = service.queryNotifyNum(1);
        int pageNum = count % pageSize == 0 ? count / pageSize : count / pageSize + 1;
        model.addAttribute("pageNum", pageNum);
        model.addAttribute("current", pageIndex);
        model.addAttribute("pageSize", pageSize);
        return "notify";
    }

    @RequestMapping("/details")
    public String queryNotifyDetails(int id, Model model,HttpSession session) {
        Notify notify = service.queryNotifyById(id);
        int num = notify.getReadCount();
        User user = (User) session.getAttribute("loginUser");
        int role = user.getRole();
        int count = 0;
        if (role == 0 || role == 4){
            count = num;
        }else{
            count = num + 1;
        }
        service.notifyReadCount(id,count);
        model.addAttribute("notify", notify);
        return "notify_details";
    }
    @CheckPermission({0,4})
    @RequestMapping("/audit")
    public String queryNotifyAudit(Integer pageIndex, Integer pageSize, Model model) {
        if (pageIndex == null) {
            pageIndex = 1;
        }
        if (pageSize == null) {
            pageSize = 6;
        }
        PageHelper.startPage(pageIndex, pageSize);
        List<Notify> notifies = service.queryAllNotify(0);
        model.addAttribute("notifies", notifies);
        int count = service.queryNotifyNum(0);
        int pageNum = count % pageSize == 0 ? count / pageSize : count / pageSize + 1;
        model.addAttribute("pageNum", pageNum);
        model.addAttribute("current", pageIndex);
        model.addAttribute("pageSize", pageSize);
        return "notify_audit";
    }

    @RequestMapping("/publish")
    public String queryNotifyPublish(int id, Model model) {
        Notify notify = service.queryNotifyById(id);
        model.addAttribute("notify", notify);
        return "notify_publish";
    }

    @RequestMapping("/approval")
    public String modifyNotify(int id,HttpSession session) {
        long time = new Date().getTime();
        int state = 1;
        User user = (User) session.getAttribute("loginUser");
        int auditorId = user.getId();
        service.modifyNotify(id, time, state, auditorId);
        return "redirect:audit";
    }

    @RequestMapping("/notApproval")
    public String modifyNotApproval(int id,HttpSession session) {
        User user = (User) session.getAttribute("loginUser");
        int auditorId = user.getId();
        long time = new Date().getTime();
        int state = -1;
        service.modifyNotify(id, time, state, auditorId);
        return "redirect:audit";
    }
    @CheckPermission({0,4})
    @RequestMapping("/draft")
    public String notifyDraft(Model model) {
        List<Notify> notifies = service.queryAllNotify(-2);
        List<Notify> draft = service.queryAllNotify(-1);
        model.addAttribute("notifies", notifies);
        model.addAttribute("draft", draft);
        return "notify_draft";
    }

    @RequestMapping("/add")
    public String queryToNotifyAdd() {
        return "notify_add";
    }

    @RequestMapping("/toAdd")
    public String notifyAdd(Notify notify, HttpSession session, String submit) {
        User user = (User) session.getAttribute("loginUser");
        /*if (user == null) {
            throw new RuntimeException("请先登录");
        }*/
        if (submit.equals("添加")) {
            notify.setNotifyState(0);
        } else {
            notify.setNotifyState(-2);
        }
        notify.setCreatorId(user.getId());
        /*notify.setCreatorId(1);*/
        notify.setCreateTime(new Date().getTime());
        service.addNotify(notify);
        return "redirect:draft";
    }

    @RequestMapping("/alter")
    public String queryNotifyAlter(int id, Model model) {
        Notify notify = service.queryNotifyById(id);
        model.addAttribute("notify", notify);
        return "notify_alter";
    }

    @RequestMapping("/toAlter")
    public String notifyAdd(Notify notify) {
        notify.setNotifyState(0);
        notify.setCreateTime(new Date().getTime());
        service.alterNotify(notify);
        return "redirect:draft";
    }
    @RequestMapping("/delete")
    public String notifyDelete(int id){
        service.deleteNotify(id);
        return "redirect:draft";
    }

    @CheckPermission({0,4})
    @RequestMapping("/management")
    public String queryNotifyManagement(Integer pageIndex, Integer pageSize, Model model){
        if (pageIndex == null) {
            pageIndex = 1;
        }
        if (pageSize == null) {
            pageSize = 6;
        }
        PageHelper.startPage(pageIndex, pageSize);
        List<Notify> notifies = service.queryAllNotify(1);
        model.addAttribute("notifies", notifies);
        int count = service.queryNotifyNum(1);
        int pageNum = count % pageSize == 0 ? count / pageSize : count / pageSize + 1;
        model.addAttribute("pageNum", pageNum);
        model.addAttribute("current", pageIndex);
        model.addAttribute("pageSize", pageSize);
        return "notify_management";
    }
    @RequestMapping("/remove")
    public String notifyRemove(int id) {
        service.deleteNotify(id);
        return "redirect:management";
    }
}
